package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.entity.chapter.VideoVo;
import com.atguigu.eduservice.mapper.EduChapterMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author thomasyang
 * @since 2023-12-09
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {


    @Autowired
    private EduVideoService eduVideoService;
    @Override
    public List<ChapterVo> getChapterVideoById(String courseId) {

        //通过id获取章节列表
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        List<EduChapter> eduChapterList = baseMapper.selectList(wrapper);

        //通过id获取小节列表
        QueryWrapper<EduVideo> videoWrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);

        List<EduVideo> eduVideoList = eduVideoService.list(videoWrapper);
        //存储最后的章节列表
        List<ChapterVo> finallChapterVoList = new ArrayList<>();

        for (int i = 0; i < eduChapterList.size(); i++) {

            //每个章节
            EduChapter eduChapter = eduChapterList.get(i);
            ChapterVo chapterVo = new ChapterVo();
            //eduChapter对象值复制到ChapterVo里面
            BeanUtils.copyProperties(eduChapter, chapterVo);
            //添加章节目录到finalList
            finallChapterVoList.add(chapterVo);
            //创建集合，用于封装章节的小节
            List<VideoVo> videoList = new ArrayList<>();

            for (int j = 0; j < eduVideoList.size(); j++) {

                EduVideo eduVideo = eduVideoList.get(j);

                if (eduVideo.getChapterId().equals(eduChapter.getId())){
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo, videoVo);
                    videoList.add(videoVo);
                }
            }

            chapterVo.setChildren(videoList);
        }
        return finallChapterVoList;
    }

    @Override
    public boolean removeChapterById(String id) {

        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id", id);
        int count = eduVideoService.count(wrapper);

        //当存在小节时不能删除
        if(count> 0 ){
            throw new GuliException(20001, "存在小节不能删除");
        }else {
            int result = baseMapper.deleteById(id);

            return  result > 0 ;
        }



    }


}
